package com.htzos.admin.system.service;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.htzos.admin.system.bean.SystemMenuBase;
import com.htzos.admin.system.dao.SystemMenuBaseMapper;
import com.htzos.admin.system.dao.SystemRoleMenuMapper;
import com.htzos.common.tool.ExtQueryBean;
@Service
@Transactional(rollbackFor = Exception.class)
public class SystemMenuBaseService {
    @Autowired
    SystemMenuBaseMapper systemMenuBaseMapper;
    @Autowired
    SystemRoleMenuMapper systemRoleMenuMapper;
    
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public List<SystemMenuBase> systemMenuBaseList(SystemMenuBase systemMenuBase, ExtQueryBean extQuery){
       return systemMenuBaseMapper.systemMenuBaseList(systemMenuBase, extQuery);
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public Integer systemMenuBaseTotal(SystemMenuBase systemMenuBase, ExtQueryBean extQuery){
       return systemMenuBaseMapper.systemMenuBaseTotal(systemMenuBase, extQuery);
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public int systemMenuBaseUpdate(SystemMenuBase systemMenuBase){
       return systemMenuBaseMapper.systemMenuBaseUpdate(systemMenuBase);
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public int systemMenuBaseInsert(SystemMenuBase systemMenuBase){
       return systemMenuBaseMapper.systemMenuBaseInsert(systemMenuBase);
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public int systemMenuBaseDelete(SystemMenuBase systemMenuBase){
    	
       systemRoleMenuMapper.systemRoleMenuDeleteByMenuId(systemMenuBase.getMenuId());
       
       return systemMenuBaseMapper.systemMenuBaseDelete(systemMenuBase);
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public SystemMenuBase systemMenuBaseGet(Integer menuId){
       return systemMenuBaseMapper.systemMenuBaseGet(menuId);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public int systemMenuBaseMove(Integer menuId, Integer parentId, Integer afterId){
    	// 查询parentId下所有的菜单
		SystemMenuBase condition = new SystemMenuBase();
		condition.setParentId(parentId);
		List<SystemMenuBase> list = systemMenuBaseMapper.systemMenuBaseList(condition, null);
		// 创建新菜单
		SystemMenuBase newMenu = new SystemMenuBase();
		newMenu.setMenuId(menuId);
		newMenu.setParentId(parentId);
		int index = 0;
		if (afterId == null || afterId.intValue() == 0 || afterId.equals(parentId)) {
			// 加到第一个
			newMenu.setOrderBy(index);
			list.add(index++, newMenu);
		}
		// 如果新菜单是插在第一个，循环需要从第二个开始，所以i得初值为index
		for (int i = index; i < list.size(); i++) {
			SystemMenuBase menu = list.get(i);
			if (menu.getMenuId().equals(menuId)) {
				list.remove(i);
				// 删除一个item之后要把i减1，否则下一个循环会跳过一个item
				i--;
			} else {
				menu.setOrderBy(index++);
				if (menu.getMenuId().equals(afterId)) {
					newMenu.setOrderBy(index);
					list.add(index++, newMenu);
					// 插入一个item之后要把i加1，这样下一个循环可以跳过新加的这个item
					i++;
				}
			}
		}
    	
    	return systemMenuBaseMapper.systemMenuBaseBatchUpdate(list);
    }
}